package com.danbay.cloud.device.service.electricmeter;

import com.danbay.cloud.device.api.dto.electricmeter.EmConsumptionBetweenDto;
import com.danbay.cloud.device.api.dto.electricmeter.EmConsumptionAddDto;
import com.danbay.cloud.device.api.dto.electricmeter.EmConsumptionDto;
import com.danbay.cloud.device.api.service.electricmeter.IEmConsumptionService;
import com.danbay.cloud.device.entity.electricmeter.EmConsumption;
import com.danbay.cloud.device.mapper.electricmeter.EmConsumptionMapper;
import com.danbay.framework.exception.BusinessException;
import com.danbay.framework.utils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmConsumptionService implements IEmConsumptionService {

    @Autowired
    private EmConsumptionMapper electricmeterConsumptionMapper;

    @Override
    public Integer insert(EmConsumptionAddDto ecaDto) {
        EmConsumption consumption = BeanUtils.convert(ecaDto, EmConsumption.class);
        return electricmeterConsumptionMapper.insert(consumption);
    }

    @Override
    public EmConsumptionDto getConsumption(EmConsumptionBetweenDto consumptionQueryDto) throws
            BusinessException {
        EmConsumptionDto ecDto = null;
        if (1 == consumptionQueryDto.getType()) {
            ecDto = selectConsumptionForDate(consumptionQueryDto);
        } else if (2 == consumptionQueryDto.getType()) {
            ecDto = selectConsumptionForMonth(consumptionQueryDto);
        } else if (3 == consumptionQueryDto.getType()) {
            ecDto = selectConsumptionForYear(consumptionQueryDto);
        } else {
            throw new BusinessException("不支持的账单类型查询");
        }
        return ecDto;
    }


    @Override
    public Double getConsumptionBetween(EmConsumptionBetweenDto consumptionQueryDto) {
        return electricmeterConsumptionMapper.selectConsumptionBetween(consumptionQueryDto);
    }

    public EmConsumptionDto selectConsumptionForDate(EmConsumptionBetweenDto consumptionQueryDto) {
        //TODO 缓存
        return electricmeterConsumptionMapper.selectConsumptionForDate(consumptionQueryDto);
    }

    public EmConsumptionDto selectConsumptionForMonth(EmConsumptionBetweenDto consumptionQueryDto) {
        return electricmeterConsumptionMapper.selectConsumptionForMonth(consumptionQueryDto);
    }

    public EmConsumptionDto selectConsumptionForYear(EmConsumptionBetweenDto consumptionQueryDto) {
        return electricmeterConsumptionMapper.selectConsumptionForYear(consumptionQueryDto);
    }
}
